package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"sy/models"
)

type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}
type SyController struct {
	beego.Controller
}

func (c *SyController) Add() {
	title := c.GetString("title")
	time := c.GetString("time")
	types := c.GetString("types")
	sy := models.Sy{
		Title: title,
		Time:  time,
		Types: types,
	}
	err := models.DB.Model(&models.Sy{}).Create(&sy).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "添加成功",
			Data: sy,
		}
		c.ServeJSON()
	}
}
func (c *SyController) List() {
	var sy []models.Sy
	err := models.DB.Model(&models.Sy{}).Find(&sy)
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "查询失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "查询成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
func (c *SyController) Up() {
	id, _ := c.GetInt("id")
	title := c.GetString("title")
	time := c.GetString("time")
	types := c.GetString("types")
	sy := models.Sy{
		Title: title,
		Time:  time,
		Types: types,
	}
	err := models.DB.Model(&models.Sy{}).Where("ID=?", id).Updates(&sy).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "修改失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "修改成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
